package com.ruoyi.iot.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.iot.domain.IotRulesTriggerTerm;
import com.ruoyi.iot.service.IIotRulesTriggerTermService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 规则触发项Controller
 *
 * @author ruoyi
 * @date 2025-03-28
 */
@RestController
@RequestMapping("/iot/term")
public class IotRulesTriggerTermController extends BaseController {
    @Autowired
    private IIotRulesTriggerTermService iotRulesTriggerTermService;

    /**
     * 查询规则触发项列表
     */
    @GetMapping("/list")
    public TableDataInfo list(IotRulesTriggerTerm iotRulesTriggerTerm) {
        startPage();
        List<IotRulesTriggerTerm> list = iotRulesTriggerTermService.selectIotRulesTriggerTermList(iotRulesTriggerTerm);
        return getDataTable(list);
    }

    /**
     * 导出规则触发项列表
     */
    @Log(title = "规则触发项", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, IotRulesTriggerTerm iotRulesTriggerTerm) {
        List<IotRulesTriggerTerm> list = iotRulesTriggerTermService.selectIotRulesTriggerTermList(iotRulesTriggerTerm);
        ExcelUtil<IotRulesTriggerTerm> util = new ExcelUtil<IotRulesTriggerTerm>(IotRulesTriggerTerm.class);
        util.exportExcel(response, list, "规则触发项数据");
    }

    /**
     * 获取规则触发项详细信息
     */
    @GetMapping(value = "/{termId}")
    public AjaxResult getInfo(@PathVariable("termId") Long termId) {
        return success(iotRulesTriggerTermService.selectIotRulesTriggerTermByTermId(termId));
    }

    /**
     * 新增规则触发项
     */
    @Log(title = "规则触发项", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody IotRulesTriggerTerm iotRulesTriggerTerm) {
        return toAjax(iotRulesTriggerTermService.insertIotRulesTriggerTerm(iotRulesTriggerTerm));
    }

    /**
     * 修改规则触发项
     */
    @Log(title = "规则触发项", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody IotRulesTriggerTerm iotRulesTriggerTerm) {
        return toAjax(iotRulesTriggerTermService.updateIotRulesTriggerTerm(iotRulesTriggerTerm));
    }

    /**
     * 删除规则触发项
     */
    @Log(title = "规则触发项", businessType = BusinessType.DELETE)
    @DeleteMapping("/{termIds}")
    public AjaxResult remove(@PathVariable Long[] termIds) {
        return toAjax(iotRulesTriggerTermService.deleteIotRulesTriggerTermByTermIds(termIds));
    }
}
